const VideoParam = require("../models/VideoParam");

module.exports = class VideoParamManager {
  /**
   * 增加一个VideoParam
   * @param {*} VideoParam
   * @returns
   */
  static async addVideoParam(paramObj) {
    const _this = this;
    const find_res = await _this.getVideoParamById(paramObj.id);
    if (find_res) {
      const new_params = { ...paramObj };
      const id = new_params.id;
      delete new_params.id;
      const result = await _this.updateVideoParam(id, new_params);
      return result.toJSON();
    } else {
      const result = await VideoParam.create(paramObj);
      return result.toJSON();
    }
  }

  static async addVideoParam2(paramObj) {
    const result = await VideoParam.create(paramObj);
    return result.toJSON();
  }

  /**
   * 删除一个ImgParam
   * @param {*} paramId
   */
  static async deleteVideoParam(paramId) {
    const result = await VideoParam.destroy({
      where: {
        id: paramId,
      },
    });
  }
  /**
   * 批量删除ImgParam
   * @param {*} id
   */
  static async bulkDeleteVideoParams(params) {
    let promises;
    const _this = this;
    const delRes = [];
    if (params.length > 0) {
      promises = params.map(async (item) => {
        const res = await _this.deleteVideoParam(item);
        delRes.push(res);
      });
    }
    await Promise.all(promises);
  }

  /**
   * 更新videoparam
   * @param {*} paramId
   * @param {*} paramObj
   */
  static async updateVideoParam(paramId, paramObj) {
    await VideoParam.update(paramObj, {
      where: {
        id: paramId,
      },
    });
    const result = await VideoParam.findOne({
      where: {
        id: paramId,
      },
    });
    return result;
  }

  /**
   * 查询所有的videoparam
   * @returns
   */
  static async getAllVideoParams() {
    const result = await VideoParam.findAll();
    return JSON.parse(JSON.stringify(result));
  }

  /**
   * 根据id查询videoparam
   * @returns
   */
  static async getVideoParamById(id) {
    const result = await VideoParam.findOne({
      where: {
        id,
      },
    });
    if (result === null) {
      console.log("getImgParamById Not found!");
    } else {
      return JSON.parse(JSON.stringify(result));
    }
  }
};
